package com.bxait;

import com.bxait.utils.ESClientSingle;
import com.bxait.utils.User;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexAction;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author Chen
 * @description: es查询数据
 * @ClassName ESTest_Client
 * @create 2023-04-08 16:16
 * @Version 1.0
 */
public class ESTest_Doc_Get {

    public static void main(String[] args) throws IOException {
        // 创建ES的客户端
        RestHighLevelClient client = ESClientSingle.getInstant();

        // 查询数据
        GetRequest request = new GetRequest();

        request.index("user").id("1001");

        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String sourceAsString = response.getSourceAsString();
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructType(User.class);
        User user = (User)mapper.readValue(sourceAsString, javaType);
        System.out.println("................................................................");
        System.out.println(user);
        // 关闭es客户端
        client.close();
    }

}
